package com.LeeCode;

import java.util.HashMap;

/**
 * 统计特殊三元组
 */

public class Code3583 {
    public static void main(String[] args) {
        int[] nums = {8, 4, 2, 8, 4};
        System.out.println(new Code3583().specialTriplets(nums));
    }

    public int specialTriplets(int[] nums) {
        int MOD = 1000000007;
        HashMap<Integer, Integer> left = new HashMap<>(), right = new HashMap<>();
        for (int num : nums) {
            right.merge(num, 1, Integer::sum);
        }
        long ans = 0;
        for (int num : nums) {
            right.merge(num, -1, (oldVal, delta) -> {
                int res = oldVal + delta;
                return res == 0 ? null : res;
            });
            int target = 2 * num;
            ans = (ans + (long) left.getOrDefault(target, 0) * right.getOrDefault(target, 0));
            left.merge(num, 1, Integer::sum);
        }
        return (int) (ans % MOD);
    }
}
